package com.jiangjiang.eshop.comment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.comment.constant.CommentType;
import com.jiangjiang.eshop.comment.constant.ShowPictures;
import com.jiangjiang.eshop.comment.dto.CommentInfoDTO;
import com.jiangjiang.eshop.comment.domain.CommentAggregateEntity;
import com.jiangjiang.eshop.comment.mapper.CommentAggregateMapper;
import com.jiangjiang.eshop.common.help.DateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.Objects;

/**
 * @ClassName CommentAggregateService
 * @Description TODO
 * @Author jhw
 * @Date 2021/5/31 20:30
 * @Version 1.0
 **/
@Service
public class CommentAggregateService extends ServiceImpl<CommentAggregateMapper, CommentAggregateEntity> {
    @Autowired
    private CommentAggregateMapper commentAggregateMapper;
    @Autowired
    private DateHelper dateHelper;

    /**
     * 新增或者更新评论聚合数据
     *
     * @param commentInfoDTO
     */
    public void saveOrUpdate(CommentInfoDTO commentInfoDTO) throws ParseException {
        CommentAggregateEntity entity = this.queryByGoodsId(commentInfoDTO.getGoodsId());
        if (Objects.isNull(entity)) {
            // 新增聚合数据
            saveCommentAggregateEntity(commentInfoDTO);

        } else {
            // 更新聚合数据
            updateCommentAggregateEntity(commentInfoDTO, entity);
        }
    }


    /**
     * 更新聚合数据
     *
     * @param commentInfoDTO 评论info DTO
     */
    private void updateCommentAggregateEntity(CommentInfoDTO commentInfoDTO, CommentAggregateEntity entity) throws ParseException {

        entity.setGmtCreate(dateHelper.getCurrentDate());
        entity.setGmtModified(dateHelper.getCurrentDate());
        entity.setTotalCommentCount(entity.getTotalCommentCount() + 1L);

        if (commentInfoDTO.getCommentType().equals(CommentType.GOOD_COMMENT)) {
            entity.setGoodCommentCount(entity.getGoodCommentCount() + 1L);
        }
        if (commentInfoDTO.getCommentType().equals(CommentType.MEDIUM_COMMENT)) {
            entity.setMediumCommentCount(entity.getMediumCommentCount() + 1L);
        }
        if (commentInfoDTO.getCommentType().equals(CommentType.BAD_COMMENT)) {
            entity.setBadCommentCount(entity.getBadCommentCount() + 1L);
        }

        double goodCommentRate = new BigDecimal(entity.getGoodCommentCount())
                .divide(new BigDecimal(entity.getTotalCommentCount()), 2, BigDecimal.ROUND_HALF_UP)
                .doubleValue();
        entity.setGoodCommentRate(goodCommentRate);

        if (ShowPictures.YES.equals(commentInfoDTO.getShowPictures())) {
            entity.setShowPicturesCommentCount(entity.getShowPicturesCommentCount() + 1L);
        }
        this.updateById(entity);
    }

    /**
     * 新增评论聚合数据
     *
     * @param commentInfoDTO
     */
    private void saveCommentAggregateEntity(CommentInfoDTO commentInfoDTO) {
        CommentAggregateEntity entity;
        entity = new CommentAggregateEntity();
        entity.setGmtCreate(new Date());
        entity.setGmtModified(new Date());
        entity.setGoodsId(commentInfoDTO.getGoodsId());
        entity.setTotalCommentCount(1L);
        // 设置默认值
        entity.setGoodCommentCount(0L);
        entity.setMediumCommentCount(0L);
        entity.setBadCommentCount(0L);


        if (commentInfoDTO.getCommentType().equals(CommentType.GOOD_COMMENT)) {entity.setGoodCommentCount(1L);}
        if (commentInfoDTO.getCommentType().equals(CommentType.MEDIUM_COMMENT)) {entity.setMediumCommentCount(1L);}
        if (commentInfoDTO.getCommentType().equals(CommentType.BAD_COMMENT)) {entity.setBadCommentCount(1L);}

        double goodCommentRate = new BigDecimal(entity.getGoodCommentCount())
                .divide(new BigDecimal(entity.getTotalCommentCount()), 2, BigDecimal.ROUND_HALF_UP)
                .doubleValue();
        entity.setGoodCommentRate(goodCommentRate);

        if (ShowPictures.YES.equals(commentInfoDTO.getShowPictures())) {
            entity.setShowPicturesCommentCount(1L);
        } else {
            entity.setShowPicturesCommentCount(0L);
        }
        this.save(entity);
    }

    /**
     * 根据商品id查询评论聚合数据
     *
     * @param goodsId 商品id
     * @return 返回结果
     */
    public CommentAggregateEntity queryByGoodsId(Long goodsId) {
        LambdaQueryWrapper<CommentAggregateEntity> eq = Wrappers.<CommentAggregateEntity>lambdaQuery()
                .eq(CommentAggregateEntity::getGoodsId, goodsId);
        return commentAggregateMapper.selectOne(eq);
    }
}
